package com.seawaterbt.ssm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.seawaterbt.ssm.entity.*;
import com.seawaterbt.ssm.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ZDatCrew {
    @Autowired
    CzDatCrewdataService datCrewdataService;
    @Autowired
    CzBasCrewService basCrewService;
    @Autowired
    ZDatCrewDataService zDatCrewDataService;

    @Autowired
    CzDatCrewdataHistoryService czDatCrewdataHistoryService;
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public void sendData() {
        QueryWrapper<CzDatCrewData> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("crew_code", 130902008).or().likeRight("crew_code", 130903007);

        List<CzDatCrewData> czDatCrewData = datCrewdataService.list(queryWrapper);
        Date date = new Date();
        for (CzDatCrewData czDatCrew : czDatCrewData) {
            CzBasCrew one = basCrewService.listOne(czDatCrew.getCrewCode());

            if (one != null && one.getCodeProvince() != null && !one.getCodeProvince().equals("")) {

                QueryWrapper wrapper = new QueryWrapper();
                wrapper.eq("CrewID", one.getCodeProvince());
                wrapper.eq("GetTime", czDatCrew.getGetTime());
                ShengDatCrewData byCodeTime = zDatCrewDataService.getByCodeTime(wrapper);
                if (byCodeTime == null) {
                    ShengDatCrewData datCrewData = new ShengDatCrewData();
                    datCrewData.setCrewId(one.getCodeProvince());
                    datCrewData.setAddTime(date);
                    datCrewData.setGetTime(czDatCrew.getGetTime());
                    datCrewData.setBackPre(Double.valueOf(czDatCrew.getBackPre()));
                    datCrewData.setBackTemp(Double.valueOf(czDatCrew.getBackTemp()));
                    datCrewData.setCumulativeHeat(Double.valueOf(czDatCrew.getCumulativeHeat()));
                    datCrewData.setInstantaneousFlow(Double.valueOf(czDatCrew.getInstantaneousFlow()));
                    datCrewData.setSupplyPre(Double.valueOf(czDatCrew.getSupplyPre()));
                    datCrewData.setSupplyTemp(Double.valueOf(czDatCrew.getSupplyTemp()));
                    zDatCrewDataService.save(datCrewData);

                }
            }

        }

        System.out.println("完成同步机组到中间库 " + df.format(new Date()));

    }

    public void getData(){
        QueryWrapper<ShengDatCrewData> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("CrewID", 130902008).or().likeRight("CrewID", 130903007);
        List<ShengDatCrewData> shengDatBoilerDataList = zDatCrewDataService.list(queryWrapper);
        for (ShengDatCrewData shengDatCrewData : shengDatBoilerDataList) {
            CzBasCrew one = basCrewService.GetOneByProviencecode(shengDatCrewData.getCrewId());
            if (one != null){
                QueryWrapper wrapper = new QueryWrapper();
                wrapper.eq("crew_code", one.getCode());
                wrapper.eq("get_time", shengDatCrewData.getGetTime());
                CzDatCrewData byCodeTime = datCrewdataService.getByCodeTime(wrapper);
                if (byCodeTime == null){
                    CzDatCrewData czDatCrewData = new CzDatCrewData();
                    czDatCrewData.setBackPre(shengDatCrewData.getBackPre()+"");
                    czDatCrewData.setBackTemp(shengDatCrewData.getBackTemp()+"");
                    czDatCrewData.setCreateTime(new Date());
                    czDatCrewData.setGetTime(shengDatCrewData.getGetTime());
                    czDatCrewData.setCrewCode(one.getCode());
                    czDatCrewData.setCumulativeHeat(shengDatCrewData.getCumulativeHeat()+"");
                    czDatCrewData.setInstantaneousFlow(shengDatCrewData.getInstantaneousFlow()+"");
                    czDatCrewData.setSupplyPre(shengDatCrewData.getSupplyPre()+"");
                    czDatCrewData.setSupplyTemp(shengDatCrewData.getSupplyTemp()+"");
                    datCrewdataService.saveOrUpdate(czDatCrewData);
                    CzDatCrewDataHistory czDatCrewDataHistory = new CzDatCrewDataHistory();
                    czDatCrewDataHistory.setBackPre(shengDatCrewData.getBackPre()+"");
                    czDatCrewDataHistory.setBackTemp(shengDatCrewData.getBackTemp()+"");
                    czDatCrewDataHistory.setCreateTime(new Date());
                    czDatCrewDataHistory.setGetTime(shengDatCrewData.getGetTime());
                    czDatCrewDataHistory.setCrewCode(one.getCode());
                    czDatCrewDataHistory.setCumulativeHeat(shengDatCrewData.getCumulativeHeat()+"");
                    czDatCrewDataHistory.setInstantaneousFlow(shengDatCrewData.getInstantaneousFlow()+"");
                    czDatCrewDataHistory.setSupplyPre(shengDatCrewData.getSupplyPre()+"");
                    czDatCrewDataHistory.setSupplyTemp(shengDatCrewData.getSupplyTemp()+"");
                    czDatCrewdataHistoryService.save(czDatCrewDataHistory);
                }
            }
        }
        System.out.println("完成机组数据拉取"+new Date());
    }
}
